/*window.addEvent('domready', function() {
	_spec_select_buy=function(){
		$$('.GoodsSearchWrap .buy-select').each(function(el) {
			var option = {
				menu: $E('.buy-select-list', el.getParent())
			};
			if (window.ie6 || window.ie7) {
				var offset = {
					relative: document.body,
					onShow: function(menu) {
						var size = el.getCoordinates(),
						fix = el.getParent('ul').getCoordinates();
						menu.setStyles({
							'left': size.left,
							'top': size.top - fix.height 
						});
					}
				};
				new DropMenu($(el), $extend(option, offset));
			} else {
				new DropMenu($(el), option);
			}
		});        
	};
	_spec_select_buy();
});
*/
window.addEvent('domready', function() {
	var base_url= Shop.base_url;
	/*加入收藏夹*/
	var MEMBER=Cookie.read('S[MEMBER]');
	var FAVCOOKIE = new Cookie('S[GFAV]['+MEMBER+']', {
		duration: 365
	});
	var _toogle = {
		'star-on': 'off',
		'star-off': 'on',
		'off': 'del',
		'on': 'add',
		'off_': 'erase',
		'on_': 'include' };
	var setStar = function(item, state, gid) {
		item.className = state == 'on' ? 'star-on': 'star-off';
		if (!gid) return;
		FAVCOOKIE.write($splat((FAVCOOKIE.read('S[GFAV]['+MEMBER+']') || '').split(','))[_toogle[state + '_']](gid).clean().join(','));
		var _type = item.get('_type') ? item.get('_type') : 'goods';
		new Request({url:base_url+'/index.php/member-ajax_' + _toogle[state] + '_fav-' + _type + '-' + gid + '.html'}).post($H({
			t: $time()
		}));
	};
	var splatFC = $splat((FAVCOOKIE.read('S[GFAV]['+MEMBER+']') || '').split(','));
	
	_fav_=function(){
		$$('li[star]').each(function(item) {
			var GID = item.get('star');
			if (splatFC.contains(GID)) {
				setStar(item, 'on');
			}
			item.addEvent('click', function(e) {
				e.stop();
				var cls = item.className ? item.className: 'star-off';
				setStar(item, _toogle[cls], GID);
			});
		});		
	};
	_fav_();
	

});

window.addEvent('domready', function() {
	/*商品对比*/
	var gc = $('goods-compare') || new Element('div').set('html', ["<div class='FormWrap goods-compare' id='goods-compare' style='display:none'>", "<div class='title'><h3>" + LANG_goodscupcake['goodsCompare'] + "<span class='close-gc' onclick='gcompare.hide()'>" + LANG_goodscupcake['close'] + "</span></h3></div>", "<form action='" + Shop.url.diff + "' method='post' target='_compare_goods'>", "<ul class='compare-box'>", "<li class='division clearfix tpl'>", "<div class='span-3'>", "<a href='' gid='{gid}' title='{gname}'>{gname}</a>", "</div>", "<span class='floatRight lnk' onclick='gcompare.erase(\"{gid}\",this);'>" + LANG_goodscupcake['del'] + "</span>", "</li>", "</ul>", "<div class='compare-bar'>", "<input type='button' name='comareing' onclick='gcompare.submit()' class='btn-compare' value=" + LANG_goodscupcake['compare'] + ">", "<input type='button' class='btn-compare' onclick='gcompare.empty()' value=" + LANG_goodscupcake['clear'] + ">", "</div>", "</form>", "</div>"].join('\n')).getFirst().inject(document.body);

	var gcBox = gc.getElement('.compare-box');
	var tpl = gc.getElement('.compare-box .tpl').get('html');
	var itemClass = 'division clearfix';
	var fixLayout = function() {
		if (gc.style.display == 'none') return;
		gc.setStyle('top', window.getScrollTop());
	};
	window.addEvents({
		'resize': fixLayout,
		'scroll': fixLayout
	});
	var GCOMPARE_COOKIE = new Cookie('S[GCOMPARE]');

	gcompare = {
		init: function() {
			var tmpC = $splat((GCOMPARE_COOKIE.read('S[GCOMPARE]') || '').split('|')).erase("").clean();

			if (tmpC.length) {
				tmpC.each(function(i) {
					this.add(JSON.decode(i), true);
				}.bind(this));
			}
		},
		hide: function() {
			gc.hide();
		},
		show: function() {
			gc.show();
			fixLayout();
		},
		add: function(dataItem, isInit) {
			this.show();
			if (!isInit) {
				var tmpC = $splat((GCOMPARE_COOKIE.read('S[GCOMPARE]') || '').split('|')).erase("").clean();
				if (tmpC.length && tmpC.some(function(i) {
					return ((JSON.decode(i)['gtype'] + '_') != (dataItem.gtype + '_'))||(JSON.decode(i)['gid']==dataItem.gid);
				})) return MessageBox.error(LANG_goodscupcake['sameerror']);

				if (tmpC.length > 4) {
					return MessageBox.error(LANG_goodscupcake['lengtherror']);
				}
				GCOMPARE_COOKIE.write(tmpC.include(JSON.encode(dataItem)).join('|'));
			}

			if (gcBox.getElement('a[gid="' + dataItem['gid'] + '"]')) return this;
			var newItem = new Element('li', {
				'class': itemClass
			}).set('html', tpl.substitute(dataItem));
			var glink = newItem.getElement('a');
			glink.set('href', dataItem.url);
			return gcBox.adopt(newItem);
		},
		erase: function(gid, el) {
			var tmpC = $splat((GCOMPARE_COOKIE.read('S[GCOMPARE]') || '').split('|')).erase("").clean();
			tmpC.each(function(i) {
				if ((JSON.decode(i) && JSON.decode(i)['gid'] + '_') == (gid + '_')) {
					tmpC.erase(i);
				}
			});
			GCOMPARE_COOKIE.write(tmpC.join('|'));
			$(el).getParent('li').remove();
		},
		empty: function() {
			GCOMPARE_COOKIE.dispose();
			gcBox.getElements('li').each(function(itm) {
				if (!itm.hasClass('tpl')) return itm.remove();
			});
		},
		submit: function() {
			gcBox.getParent('form').submit();
		}
	};
	gcompare.init();

});

